package com.sgb.kjwl.view.ui.adapter.transport;

import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.databinding.ViewDataBinding;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.transport.OperateGoodsSourcePlaceVO;
import com.sgb.kjwl.model.entity.transport.SingleItemConstant;
import com.sgb.kjwl.model.entity.transport.common.AddressItem;
import com.sgb.kjwl.utils.AbScreenUtils;
import com.sgb.kjwl.utils.CargoConstant;
import com.swgk.core.util.MDateUtil;

import java.util.List;

/**
 * @Author : 黄淋
 * @date :2021/3/11 0023$ 11:26$
 * @Description :运输订单详情地址适配器$
 */
public class TransportOrderDetailAddressAdapter extends BaseMultiItemQuickAdapter<AddressItem, BaseViewHolder> {

    private ItemOnClickListen mItemOnClickListen;
    private OperateGoodsSourcePlaceVO mPlace;

    public TransportOrderDetailAddressAdapter(@Nullable List<AddressItem> data, OperateGoodsSourcePlaceVO place) {
        super(data);
        this.mPlace = place;
        addItemType(SingleItemConstant.TYPE_ONE, R.layout.item_goods_detail_address_for_transport);
    }

    public void SetItemOnClickListen(ItemOnClickListen item) {
        this.mItemOnClickListen = item;
    }

    @Override
    protected void convert(BaseViewHolder helper, AddressItem item) {
        helper.setVisible(R.id.tv_line, item.isLines() ? true : false);
        helper.setVisible(R.id.tv_distance, item.isLines() ? true : false);
        helper.setImageResource(R.id.img_unload, item.getIcon());
        helper.setText(R.id.tv_distance, item.getDistance() == null ? "" : item.getDistance());
        helper.setText(R.id.tv_city_name, item.getAddressName());
        helper.setText(R.id.tv_detail_address, item.getAddressdetail());
        helper.setVisible(R.id.map_address, true);
        helper.getView(R.id.map_address).setOnClickListener((v) -> {
            mItemOnClickListen.onItemClick(mPlace, item.getType(), item.getPosition());
        });
        TextView tv = helper.getView(R.id.tv_line);
        helper.setVisible(R.id.tv_shipment_time, false);
        RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams) tv.getLayoutParams();
        helper.setVisible(R.id.tv_line_top, item.getPosition() != 0);
        if (item.getPosition() != 0 || item.getShipmentTime() == 0) {
            p.height = AbScreenUtils.dp2px(mContext, 70);
        } else {
            helper.setVisible(R.id.tv_shipment_time, true);
            helper.setText(R.id.tv_shipment_time, CargoConstant.stampFormatListLoadTime(true, item.getShipmentTime()
                    , item.getStartPointTime(), item.getShipmentEndTime(), item.getEndPointTime()) + " 装货");
            p.height = AbScreenUtils.dp2px(mContext, 90);
        }
        tv.setLayoutParams(p);

//        helper.setVisible(R.id.tv_distance, item.getDistance() != null && !item.getDistance().equals(""));

        if (!item.isLines() && item.getArrivalTime() != 0) {
            helper.setVisible(R.id.tv_arrive_date, true);
            helper.setText(R.id.tv_arrive_date, "开始运输后" + item.getArrivalTime() + "天内到货");
        } else {
            helper.setVisible(R.id.tv_arrive_date, false);
        }
    }

    @FunctionalInterface
    public interface ItemOnClickListen<T, B extends ViewDataBinding> {
        void onItemClick(OperateGoodsSourcePlaceVO place, String i, int position);
    }
}
